package controller;

import dataLayer.DAOGroup;
import dataLayer.DBconnect;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import model.College;
import model.Group;
import types.ConsoleType;
import view.PanelGroupDetails;
import view.PanelGroupList;
import view.PanelHolder;

public class EditGroup extends DBconnect implements ActionListener {

	private PanelGroupDetails PGDform;
	private String Group_Name;
	private String Group_Name_Old;

	private JButton JBannuleren;
	private JButton JBOpslaan;
	private JTextField JTname;
	private JButton JBWijzig;
	private JButton JBVerwijder;
	private JLabel JLname;
	private JLabel JLklas;

	public EditGroup(PanelGroupDetails PGDform) {
		this.setPGDform(PGDform);
	}

	// Check on fields for EditGroup and message in console
	public void actionPerformed(ActionEvent e) {
		openConn();
		Group g = new Group(Group_Name, Group_Name_Old);
		g = PGDform.getGroup();
		Group g2 = new Group(Group_Name, g.getGroup_name());

		if (e.getActionCommand().equals("Opslaan")) {
			String message = null;
			ConsoleType type = null;

			try {
				Group_Name = PGDform.getJTName().getText();
				g2.setGroup_name(Group_Name.toUpperCase());

				if (Group_Name.equals("")) {
					message = "Er is nog geen naam ingevuld.";
					type = ConsoleType.EMPTY;
				} else if (Group_Name.length() > 12) {
					message = "De naam van de klas mag niet langer zijn dan 12 tekens.";
					type = ConsoleType.ERROR;
				} else if (!(g2.getGroup_name().equals(g2.getGroup_name_old()))
						&& !College.getInstance().addGroup(g2)) {
					message = "Er bestaat al een docent met deze code.";
					type = ConsoleType.ERROR;
				} else {
					if (g2.getGroup_name().equals(g2.getGroup_name_old())) {
						College.getInstance().getAllGroups().remove(g);
					}
					message = Group_Name + " is aangepast.";
					type = ConsoleType.SUCCESS;

					College.getInstance().getAllGroups().remove(g);
					DAOGroup.getInstance().updateGroup(g2);
					College.getInstance().getAllGroups().add(g2);

					JBannuleren = PGDform.getJBannuleren();
					JBOpslaan = PGDform.getJBopslaan();
					JBWijzig = PGDform.getJBWijzig();
					JBVerwijder = PGDform.getJBDelete();
					JLklas = PGDform.getJLKlas();
					JLname = PGDform.getJLName();
					JTname = PGDform.getJTname();

					JBOpslaan.setVisible(false);
					JTname.setVisible(false);
					JBannuleren.setVisible(false);
					JLklas.setVisible(true);
					JLname.setVisible(true);
					JBWijzig.setVisible(true);
					JBVerwijder.setVisible(true);

					// Reload all groups repaint PanelGroupList
					College.getInstance().setAllGroups();
					PanelGroupList.getInstance().repaint();

					PanelHolder ph = PanelHolder.getInstance();
					ph.remove(PGDform);
					ph.add(PanelGroupList.getInstance());
					ph.repaint();
					ph.revalidate();

				}

			} catch (Exception e1) {
				e1.printStackTrace();
			}
			Console.getInstance().setConsole(message, type);

		}

	}

	public PanelGroupDetails getPGDform() {
		return PGDform;
	}

	public void setPGDform(PanelGroupDetails PGDform) {
		this.PGDform = PGDform;
	}

}
